﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BaseGrid
{
    public string name = "";
    public int horValue = -1;
    public int verValue = -1;
    E_MapGrid_Type grid_Type;
    GameObject floor;
    public int[] gridSign;
    public string pathTag;
    public float[] objPosition;
    Vector3 foldLineHead;//折叠线头部
    Vector3 foldLineTail;//折叠线尾部
    Vector3 foldLineVector;//折叠线向量
    List<int[]> lines = new List<int[]>();
    Vector3 agLeftUp;
    Vector3 agLeftDown;
    Vector3 agRightUp;
    Vector3 agRightDown;
    Vector3 forkLeft;
    Vector3 forkRight;
    Vector3[] agAndFork = new Vector3[7];
    public E_MapGrid_Type E_Grid_Type { get => grid_Type; set => grid_Type = value; }
    public GameObject Floor { get => floor; set => floor = value; }
    public Vector3 FoldLineVector { get => foldLineVector; set => foldLineVector = value; }
    public Vector3 AgLeftUp { get => agLeftUp; set => agLeftUp = value; }
    public Vector3 AgLeftDown { get => agLeftDown; set => agLeftDown = value; }
    public Vector3 AgRightUp { get => agRightUp; set => agRightUp = value; }
    public Vector3 AgRightDown { get => agRightDown; set => agRightDown = value; }
    public Vector3 ForkLeft { get => forkLeft; set => forkLeft = value; }
    public Vector3 ForkRight { get => forkRight; set => forkRight = value; }
    public Vector3[] AgAndFork { get => agAndFork; set => agAndFork = value; }
    public List<int[]> Lines { get => lines; set => lines = value; }

    public BaseGrid()
    {

    }
    public BaseGrid(GameObject floor, Material material, Vector3 pavedPositon, Transform transform, int[] sign)
    {
        name = sign[0] + "*" + sign[1];
        gridSign = sign;
        horValue = gridSign[0];
        verValue = gridSign[1];
        this.Floor = GameObject.Instantiate(floor, pavedPositon, Quaternion.identity, transform);
        this.Floor.GetComponent<MeshRenderer>().material = material;
        this.Floor.name = this.name;
        objPosition = new float[2] { this.Floor.transform.position.z, this.Floor.transform.position.x };
        foldLineHead = new Vector3((pavedPositon.x - 0.5f), 0.3f, pavedPositon.z + 0.5f);
        foldLineTail = new Vector3((pavedPositon.x + 0.5f), 0.3f, pavedPositon.z - 0.5f);
        AgLeftUp = new Vector3((pavedPositon.x - 0.5f), 0.3f, pavedPositon.z + 0.5f);
        AgLeftDown = new Vector3((pavedPositon.x - 0.5f), 0.3f, pavedPositon.z - 0.5f);
        AgRightUp = new Vector3((pavedPositon.x + 0.5f), 0.3f, pavedPositon.z + 0.5f);
        AgRightDown = new Vector3((pavedPositon.x + 0.5f), 0.3f, pavedPositon.z - 0.5f);
        ForkLeft = AgLeftUp - AgRightDown;
        ForkRight = AgRightUp - AgLeftDown;
        FoldLineVector = foldLineHead - foldLineTail;
        AgAndFork[0] = AgLeftUp;
        AgAndFork[1] = AgRightUp;
        AgAndFork[2] = AgLeftDown;
        AgAndFork[3] = AgRightDown;
        AgAndFork[4] = new Vector3(pavedPositon.x, 0.3f, pavedPositon.z); ;
        AgAndFork[5] = ForkLeft;
        AgAndFork[6] = ForkRight;
    }
    public void AddGridLineList(int[] line)
    {
        Lines.Add(line);
    }
    public void ChangeMaterial(Material material)
    {
        Floor.GetComponent<MeshRenderer>().material = material;
    }
    public void DebugAllLineInGrid()
    {
        // if (Lines.Count > 0)
        // {
        //     for (int i = 0; i < Lines.Count; i++)
        //     {
        //         Debug.Log($"{Floor.name}   {Lines[i][0]}    {Lines[i][1]}");
        //     }
        // }
        // else
        // {
        //     Debug.Log("方块没有线");
        // }
    }
    public void RemoveLins(int[] line)
    {
        for (int i = 0; i < Lines.Count; i++)
        {
            if (Lines[i][0] == line[0] && Lines[i][1] == line[1])
            {
                Lines.RemoveAt(i);
            }
        }
    }
}

public enum E_MapGrid_Type
{
    Type_default,
    Type_obstacle,
    Type_noPass,
}